﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace LGPSDataGen.Generators
{
    class StockGenerator : Generator, IGenerator
    {
        private string pathToFile;
        //Use Yahoo as the open data source to download
        //Note this file comes in in reverse order (From end date to start date)
        private string content = "http://ichart.finance.yahoo.com/table.csv?s=%5EDJI&a=00&b=1&c=2000&d=11&e=8&f=2020&g=d&ignore=.csv";
        private DateTime mDate;

        public StockGenerator(DateTime theDate)
        {
            mDate = theDate;
            string timeStamp = mDate.Year + "_" + mDate.Month + "_" + mDate.Day;
            pathToFile = @"Stock" + timeStamp + ".csv";
        }

        public override void DownloadData()
        {
            WebClient wc = new WebClient();
            wc.DownloadFile(content, pathToFile);
        }

        public override void ProcessData()
        {
            StreamReader stream = new StreamReader(pathToFile + "2.csv");
            double value = 0.0; //Defaulted to 0.0 in case it is a weekend or it is closed
            string fullDate = mDate.Year + "_" + mDate.Month + "_" + mDate.Day;

            stream.ReadLine();

            //Find the specified date and get the correct values
            while (!stream.EndOfStream)
            {
                string temp = stream.ReadLine();
                string date = temp.Split(',')[0];
                int month = Int32.Parse(date.Split('-')[1]);
                int day = Int32.Parse(date.Split('-')[2]);
                int year = Int32.Parse(date.Split('-')[0]);

                DateTime d = new DateTime(year, month, day);
                if (d.DayOfYear == mDate.DayOfYear && year == mDate.Year)
                {
                    value = Double.Parse(temp.Split(',')[1]);
                    break;
                }
            }

            //Record the values for the day
            List<string> FileToBuild = new List<string>();
            for (int i = 0; i < 24; i++)
            {
                string dayBreakDown = i + "," + value;
                FileToBuild.Add(dayBreakDown);
            }
            File.WriteAllLines(@"Stock3-" + fullDate + ".csv", FileToBuild.ToArray());
            stream.Close();
        }

        public override void ExtractData()
        {
            StreamReader stream = new StreamReader(pathToFile);
            List<string> fileToBuild = new List<string>();
            
            stream.ReadLine();

            while (!stream.EndOfStream)
            {
                string temp = stream.ReadLine();
                string date = temp.Split(',')[0];
                string open = temp.Split(',')[1];
                string close = temp.Split(',')[4];
                //Use the absolute value of the difference of the start and close for the day
                double change = Math.Abs(1 - (Double.Parse(open) / Double.Parse(close))); 
                temp = date + "," + change;
                fileToBuild.Insert(0, temp);
            }
            File.WriteAllLines(pathToFile + "2.csv", fileToBuild.ToArray());

            stream.Close();
        }
    }
}
